//
//  ScreenUiView.m
//  lsOA
//
//  Created by 蓝水软件 on 16/8/2.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "ScreenUiView.h"
#import "GetData1.h"

@implementation ScreenUiView
{
    HZQDatePickerView *pikerView;
    UIScrollView *MainscrollView;
    NSDictionary *dic;
    UIView *container;
    UITextField *screenTextField;
    UITextField *starTime;
    UITextField *endTime;
    GetData1 *chooseView;
}


- (instancetype)initWithFromView:(NSString *)fromView
{
    self = [super init];
    if (self) {
        _selfFromView = fromView;
        self.seletRow = @"4";
        self.backgroundColor = [UIColor colorWithWhite:0.010 alpha:0.4];
        container = [[UIView alloc]init];
        [self addSubview:container];
        container.backgroundColor = [UIColor whiteColor];
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.right.mas_equalTo(-30);

            make.centerY.equalTo(self);
        }];
        container.layer.cornerRadius = 10;
        
        UILabel *title = [[UILabel alloc]init];
        [container addSubview:title];
        title.text = @"筛选";
        title.textColor = [UIColor colorWithRed:0.924 green:0.000 blue:0.009 alpha:1.000];
        title.font = FONT(20);
        title.textAlignment = NSTextAlignmentCenter;
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(5*FitSize);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(LRLCGSizeMake(KSWidth, 30));
        }];;
        
        //筛选内容
        screenTextField = [[UITextField alloc]init];
        [container addSubview:screenTextField];
        screenTextField.backgroundColor = [UIColor whiteColor];
        screenTextField.placeholder = @"筛选内容";
        screenTextField.layer.borderWidth = 1;
        screenTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        screenTextField.textAlignment = NSTextAlignmentCenter;
        [screenTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(title.mas_bottom).offset(10*FitSize);
            make.left.mas_equalTo(15*FitSize);
            make.right.mas_equalTo(-15*FitSize);
            make.height.mas_equalTo(30*FitSize);
        }];
        starTime = [[UITextField alloc]init];
        starTime.tag = 600+1;
        starTime.backgroundColor = [UIColor whiteColor];
        starTime.layer.borderWidth = 1;
        starTime.layer.borderColor = [UIColor lightGrayColor].CGColor;
        starTime.placeholder = @"请选择开始时间";
        starTime.textAlignment = NSTextAlignmentCenter;
        [container addSubview:starTime];
        starTime.delegate = self;
        [starTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(screenTextField.mas_bottom).offset(30);
            make.left.equalTo(screenTextField);
            make.right.equalTo(screenTextField);
            make.height.mas_equalTo(30*FitSize);
        }];
        
        endTime = [[UITextField alloc]init];
        endTime.placeholder = @"请选择结束时间";
        endTime.layer.borderWidth = 1;
        endTime.layer.borderColor = [UIColor lightGrayColor].CGColor;
        endTime.textAlignment = NSTextAlignmentCenter;
        endTime.backgroundColor = [UIColor whiteColor];
        [container addSubview:endTime];
        endTime.delegate = self;
        endTime.tag = 600+2;
        [endTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(starTime.mas_bottom).offset(30);
            make.left.equalTo(screenTextField);
            make.right.equalTo(screenTextField);
            make.height.mas_equalTo(30*FitSize);
        }];
        
        
        _classification = [[UITextField alloc]init];
        [container addSubview:_classification];
        _classification.delegate = self;
        _classification.textAlignment = NSTextAlignmentCenter;
        _classification.layer.borderWidth = 1;
        _classification.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [_classification mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(endTime.mas_bottom).offset(30);
            make.left.equalTo(screenTextField);
            make.right.equalTo(screenTextField);
            make.height.mas_equalTo(30*FitSize);
        }];
        _classification.placeholder = @"请选择分类";

        if ([fromView intValue] == 2 || [fromView integerValue] == 3 || [fromView integerValue] == 4) {
           
        }else{
            [_classification mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];

        }
        
        
        
        UIButton *sure  = [UIButton buttonWithType:UIButtonTypeCustom];
        sure.layer.borderWidth = 1;
        sure.layer.borderColor = [UIColor colorWithRed:0.137 green:0.767 blue:0.580 alpha:1.000].CGColor;
        sure.layer.cornerRadius = 5;
        sure.backgroundColor = [UIColor whiteColor];
        [sure addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
        [sure setTitle:@"确定" forState:UIControlStateNormal];
        [sure setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [container addSubview:sure];
        [sure mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_classification.mas_bottom).offset(30);
            make.left.equalTo(endTime);
            make.right.equalTo(endTime);
            make.height.mas_equalTo(25*FitSize);
            make.centerX.equalTo(container);
            
        }];
        
        UIButton *cancel  = [UIButton buttonWithType:UIButtonTypeCustom];
        cancel.layer.borderWidth = 1;
        cancel.layer.borderColor = [UIColor colorWithRed:0.137 green:0.767 blue:0.580 alpha:1.000].CGColor;
        cancel.layer.cornerRadius = 5;
        cancel.backgroundColor = [UIColor whiteColor];
        [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
        [cancel setTitle:@"取消" forState:UIControlStateNormal];
        [cancel setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [container addSubview:cancel];
        [cancel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(sure.mas_bottom).offset(30);
            make.left.equalTo(endTime);
            make.right.equalTo(endTime);
            make.height.equalTo(sure);
            make.centerX.equalTo(container);
        }];
        
        [container mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(cancel.mas_bottom).offset(30);
        }];
        
    }
    return self;
}
//确定按钮
- (void)sure{
    [self removeFromSuperview];
    dic = @{@"conditions":screenTextField.text,@"starTime":starTime.text,@"endTime":endTime.text,@"verify":self.seletRow};
    self.block(dic);
}

//取消按钮
- (void)cancel{
    [self removeFromSuperview];
}

- (void)hidden{
    [self removeFromSuperview];
}
//初始化时间选择器
- (void)setupDateView:(DateType)type {
    
    pikerView = [HZQDatePickerView instanceDatePickerView];
    pikerView.frame = CGRectMake(0, 0, self.bounds.size.width,self.bounds.size.height);
    
    [pikerView setBackgroundColor:[UIColor clearColor]];
    pikerView.delegate = self;
    pikerView.type = type;
    // 今天开始往后的日期
    //    [pikerView.datePickerView setMinimumDate:[NSDate date]];
    // 在今天之前的日期
    //    [_pikerView.datePickerView setMaximumDate:[NSDate date]];
    [self addSubview:pikerView];
}
//时间选择器按钮代理
- (void)getSelectDate:(NSString *)date type:(DateType)type{
    NSLog(@"%d - %@", type, date);
    
    switch (type) {
        case DateTypeOfStart:{
            starTime.text = [NSString stringWithFormat:@"%@", date];
        }
            break;
            
        case DateTypeOfEnd:{
            endTime.text = [NSString stringWithFormat:@"%@", date];
        }
            break;
            
        default:
            break;
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [screenTextField resignFirstResponder];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == starTime) {
        [self setupDateView:DateTypeOfStart];
    }else if(textField == endTime){
        [self setupDateView:DateTypeOfEnd];
    }else if (textField == _classification){
 
        NSArray *arr = [NSArray array];
        if ([_selfFromView intValue] == 3) {
            arr = @[@"我的批阅",@"已经批阅",@"我的申请",@"全部列表"];
        }else if ([_selfFromView intValue] == 2){
            arr = @[@"我参与的",@"我创建的",@"与我有关"];
        }else if ([_selfFromView intValue] == 4){
            arr = @[@"我执行的",@"我管理的",@"我创建的",@"所有"];
        }
        chooseView = [[GetData1 alloc]initWithTitle:@"列表分类" andArr:arr];
        [chooseView show];
        __weak typeof (GetData1 ) *c = chooseView;
        __weak typeof (ScreenUiView ) *screen = self;

        chooseView.block = ^(NSString *title,NSString *index){
            screen.classification.text = title;
            screen.seletRow = index;
            [c hide];
            
        };
    
    }
    return NO;
}
@end
